package com.hehui.module.customer.controller;

import com.hehui.common.core.commom.AjaxResult;
import com.hehui.common.core.controller.BaseController;
import com.hehui.common.utils.SecurityUtils;
import com.hehui.common.utils.StringUtils;
import com.hehui.module.customer.entity.CustomLocale;
import com.hehui.module.customer.service.ICustomLocaleService;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.Iterator;
import java.util.List;

/*
 *归属地
 */
@RestController
@RequestMapping("/customlocale")
public class CustomLocaleController extends BaseController {
    @Autowired
    private ICustomLocaleService customLocaleService;

    //获取区域列表
    @GetMapping("/list")
    public AjaxResult list(CustomLocale customLocale) {
        if (customLocale.getMerchantId() == 0) {
            customLocale.setMerchantId(SecurityUtils.getMerchantId());
        }
        List<CustomLocale> customLocales = customLocaleService.selectAllCustomLocale(customLocale);
        return AjaxResult.success(customLocales);
    }

    //获取部门列表(排除节点)
    @GetMapping("/list/exclude/{localeId}")
    public AjaxResult excludeChild(@PathVariable(value = "localeId", required = false) Long localeId) {
        CustomLocale customLocale = new CustomLocale();
        customLocale.setMerchantId(SecurityUtils.getMerchantId());
        List<CustomLocale> customLocales = customLocaleService.selectAllCustomLocale(customLocale);
        Iterator<CustomLocale> it = customLocales.iterator();
        while (it.hasNext()) {
            CustomLocale d = (CustomLocale) it.next();
            if (d.getLocaleId().longValue() == localeId || ArrayUtils.contains(StringUtils.split(d.getAncestors(), ","), localeId + "")) {
                it.remove();
            }
        }
        return AjaxResult.success(customLocales);
    }

    //根据ID获取详细信息
    @GetMapping(value = "/{localeId}")
    public AjaxResult getInfoById(@PathVariable(value = "localeId", required = false) Long localeId) {
        return AjaxResult.success(customLocaleService.selectCustomLocaleById(localeId));
    }

    /**
     * 获取节点下拉树列表
     */
    @GetMapping("/treeselect")
    public AjaxResult treeselect(CustomLocale customLocale) {
        List<CustomLocale> customLocales = customLocaleService.selectAllCustomLocale(customLocale);
        return AjaxResult.success(customLocaleService.buildCustomLocaleTreeSelect(customLocales));
    }


    //添加
    @PostMapping("/add")
    public AjaxResult add(@Validated @RequestBody CustomLocale customLocale) {
        //设置商户 ID
        customLocale.setMerchantId(SecurityUtils.getMerchantId());
        //节点名的唯一性
        if (customLocaleService.checkCustomLocaleNameUnique(customLocale.getName(), customLocale.getMerchantId())) {
            return AjaxResult.error("添加区域'" + customLocale.getName() + "'失败，区域名称已存在");
        }
        if (customLocale.getParentId() == customLocale.getLocaleId()) {
            return AjaxResult.error("添加区域'" + customLocale.getName() + "'失败，上级区域不能是自己");
        }
        if (customLocaleService.checkCustomLocaleNumUnique(customLocale.getNum(), customLocale.getMerchantId())) {
            return AjaxResult.error("添加区域'" + customLocale.getName() + "'失败，" + customLocale.getNum() + "编号已存在");
        }

        return toAjax(customLocaleService.insertCustomLocale(customLocale));
    }

    //修改节点信息
    @PutMapping("/update")
    public AjaxResult updateCustomLocale(@Validated @RequestBody CustomLocale customLocale) {
        CustomLocale old = customLocaleService.selectCustomLocaleById(customLocale.getLocaleId());

        if (!old.getName().equals(customLocale.getName())) {
            if (customLocaleService.checkCustomLocaleNameUnique(customLocale.getName(), customLocale.getMerchantId())) {
                return AjaxResult.error("修改区域'" + customLocale.getName() + "'失败，区域名称已存在");
            }
        }
        if (!old.getNum().equals(customLocale.getNum())) {
            if (customLocaleService.checkCustomLocaleNumUnique(customLocale.getNum(), customLocale.getMerchantId())) {
                return AjaxResult.error("修改区域'" + customLocale.getName() + "'失败，" + customLocale.getNum() + "编号已存在");
            }
        }

        if (customLocale.getParentId() == customLocale.getLocaleId()) {
            return AjaxResult.error("修改区域'" + customLocale.getName() + "'失败，上级区域不能是自己");
        }
        return toAjax(customLocaleService.updateCustomLocale(customLocale));
    }

    //删除
    @DeleteMapping(value = {"/", "/{localeId}"})
    public AjaxResult deleteCustomLocaleById(@PathVariable Long localeId) {

        Long merchantId = SecurityUtils.getMerchantId();

        if (customLocaleService.hasChildByCustomLocaleById(localeId, merchantId)) {
            return AjaxResult.error("存在子区域，不允许删除");
        }
        if (customLocaleService.checkCustomLocaleExistCustomer(localeId, merchantId)) {
            return AjaxResult.error("该区域下存在用户，不允许删除");
        }
        return toAjax(customLocaleService.deleteCustomLocaleById(localeId));
    }

    //通过商户ID查询设置编号
    @GetMapping("/getnum")
    public AjaxResult createLocaleNum() {
        return AjaxResult.success("成功",customLocaleService.createNewNum(SecurityUtils.getMerchantId()));
    }
}
